﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Core.Common;
using ECommerce.Model.Product;

namespace ECommerce.DAL.Product
{
    public class ECProductDAL : BaseDAL
    {
        public List<ECProductInfo> GetFavoriteList(int memberID)
        {
            string sql = "select * from ECProduct where ID in (select ProductID from ECFavorite where MemberID=@p0)";
            List<ECProductInfo> list = dbOper.Query<ECProductInfo>(sql, memberID);
            return list;
        }

        public void DeleteFavorite(int memberID, int productID)
        {
            string sql = "delete from ECFavorite where MemberID=@p0 and ProductID=@p1";
            dbOper.RunSql(sql, memberID, productID);
        }

        /// <summary>
        /// 取出销量靠前的产品
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<ECProductInfo> GetTopList(int count)
        {
            string sql = "select top " + count + " * from ECProduct where State = " + (int)ProductState.上架 + " order by SalesVolume desc";
            List<ECProductInfo> list = dbOper.Query<ECProductInfo>(sql);
            return list;
        }

        public List<ECProductInfo> GetNewList(int count)
        {
            string sql = "select top " + count + " * from ECProduct where State = " + (int)ProductState.上架 + " order by InputTime desc";
            List<ECProductInfo> list = dbOper.Query<ECProductInfo>(sql);
            return list;
        }

        public List<ECProductInfo> GetRecommendList(int count)
        {
            string sql = "select top " + count + " * from ECProduct where State = " + (int)ProductState.上架 + " and Recommend=1 order by InputTime desc";

            List<ECProductInfo> list = dbOper.Query<ECProductInfo>(sql);
            return list;
        }

        public void AddToFavorite(int memberID, int productID)
        {
            string sql = "select count(*) from ECFavorite where MemberID=@p0 and ProductID=@p1";
            int count = dbOper.GetScalar<int>(sql, memberID, productID);
            if (count == 0)
            {
                sql = "insert into ECFavorite(MemberID,ProductID) values(@p0,@p1)";
                dbOper.RunSql(sql, memberID, productID);
            }
        }
    }

}
